package src.view;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import com.jgoodies.forms.layout.*;
import org.jdesktop.swingx.*;

/*
 * Created by JFormDesigner on Sun May 23 15:44:48 ART 2010
 */

import src.controller.ManageUsersController;
import src.resource.PublicResource;




/**
 * @author Guillermo Exposito
 */
public class ManageUsers extends JPanel {
	

	private static final long serialVersionUID = 1L;
	private ManageUsersController controller;
	private static ManageUsers instance=null;
	private final String  PATH=PublicResource.getPath();
	
	
	
	private ManageUsers(ManageUsersController controller) {
		this.controller=controller;
		initComponents();
		this.btnSearch.addActionListener(new ActionSearchUsers());
		this.btnDelete.addActionListener(new ActionDeleteUser());
		this.jListUsers.addListSelectionListener(new ActionSelectItem());
		this.btnSave.addActionListener(new ActionUpdateUser());
		
	}
	
	public static ManageUsers getInstance(ManageUsersController controller){
		if(instance==null)
			instance=new ManageUsers(controller);
		return instance;
	}

	private void initComponents() {
		// JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
		// Generated using JFormDesigner Evaluation license - Guillermo Exposito
		panel2 = new JPanel();
		lblTitle = new JLabel();
		panel1 = new JPanel();
		jPanelSearchUser = new JPanel();
		lblSearchUser = new JLabel();
		label1 = new JLabel();
		txtSearch = new JTextField();
		btnSearch = new JButton();
		scrollPane1 = new JScrollPane();
		jListUsers = new JList();
		btnDelete = new JButton();
		jPanelFields = new JPanel();
		lblName = new JLabel();
		txtName = new JTextField();
		label4 = new JLabel();
		lblUserName = new JLabel();
		txtUserName = new JTextField();
		label5 = new JLabel();
		lblCountry = new JLabel();
		txtCountry = new JTextField();
		lblCity = new JLabel();
		txtCity = new JTextField();
		lblAddress = new JLabel();
		txtAddress = new JTextField();
		lblPhoneNumber = new JLabel();
		txtPhoneNumber = new JTextField();
		label2 = new JLabel();
		chkActive = new JCheckBox();
		label3 = new JLabel();
		btnSave = new JButton();
		CellConstraints cc = new CellConstraints();

		//======== this ========

		
		setLayout(new FormLayout(
			"left:default:grow",
			"fill:default, fill:default:grow"));

		//======== panel2 ========
		{
			panel2.setLayout(new FormLayout(
				"left:440dlu",
				"10dlu, fill:default, 5dlu"));

			//---- lblTitle ----
			lblTitle.setText("Manage users");
			lblTitle.setFont(new Font("Dialog", Font.PLAIN, 18));
			panel2.add(lblTitle, cc.xywh(1, 2, 1, 1, CellConstraints.CENTER, CellConstraints.DEFAULT));
		}
		add(panel2, cc.xy(1, 1));

		//======== panel1 ========
		{
			panel1.setLayout(new HorizontalLayout(1));

			//======== jPanelSearchUser ========
			{
				jPanelSearchUser.setLayout(new FormLayout(
					"20dlu, 22dlu, 3dlu, 100dlu, 3dlu, $lcgap, 62dlu, $lcgap, 5dlu",
					"2*(default, 10dlu), fill:default:grow, $lgap, 10dlu, $lgap, default, $lgap, 10dlu"));

				//---- lblSearchUser ----
				lblSearchUser.setText("Search user");
				lblSearchUser.setFont(new Font("Dialog", Font.PLAIN, 14));
				jPanelSearchUser.add(lblSearchUser, cc.xywh(2, 1, 3, 1));

				//---- label1 ----
				label1.setText("Name");
				jPanelSearchUser.add(label1, cc.xywh(2, 3, 1, 1, CellConstraints.RIGHT, CellConstraints.DEFAULT));
				jPanelSearchUser.add(txtSearch, cc.xy(4, 3));

				//---- btnSearch ----
				btnSearch.setText("Search");
				jPanelSearchUser.add(btnSearch, cc.xywh(7, 3, 3, 1, CellConstraints.LEFT, CellConstraints.DEFAULT));

				//======== scrollPane1 ========
				{

					//---- jListUsers ----
					jListUsers.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
					scrollPane1.setViewportView(jListUsers);
				}
				jPanelSearchUser.add(scrollPane1, cc.xywh(2, 5, 6, 1, CellConstraints.FILL, CellConstraints.FILL));

				//---- btnDelete ----
				btnDelete.setText("Delete");
				jPanelSearchUser.add(btnDelete, cc.xywh(7, 9, 1, 1, CellConstraints.RIGHT, CellConstraints.DEFAULT));
			}
			panel1.add(jPanelSearchUser);

			//======== jPanelFields ========
			{
				jPanelFields.setBorder(null);
				jPanelFields.setLayout(new FormLayout(
					"60dlu, $lcgap, 150dlu, 5dlu, 10dlu",
					"50dlu, 7*($lgap, default), $lgap, 10dlu, $lgap, default, $lgap, 10dlu"));

				//---- lblName ----
				lblName.setText("Name");
				lblName.setHorizontalAlignment(SwingConstants.RIGHT);
				jPanelFields.add(lblName, cc.xy(1, 3));
				jPanelFields.add(txtName, cc.xy(3, 3));

				//---- label4 ----
				label4.setText("*");
				label4.setForeground(Color.red);
				jPanelFields.add(label4, cc.xy(4, 3));

				//---- lblUserName ----
				lblUserName.setText("User name");
				lblUserName.setHorizontalAlignment(SwingConstants.RIGHT);
				jPanelFields.add(lblUserName, cc.xy(1, 5));
				jPanelFields.add(txtUserName, cc.xy(3, 5));

				//---- label5 ----
				label5.setText("*");
				label5.setForeground(Color.red);
				jPanelFields.add(label5, cc.xy(4, 5));

				//---- lblCountry ----
				lblCountry.setText("Country");
				lblCountry.setHorizontalAlignment(SwingConstants.RIGHT);
				jPanelFields.add(lblCountry, cc.xy(1, 7));
				jPanelFields.add(txtCountry, cc.xy(3, 7));

				//---- lblCity ----
				lblCity.setText("City");
				lblCity.setHorizontalAlignment(SwingConstants.RIGHT);
				jPanelFields.add(lblCity, cc.xy(1, 9));
				jPanelFields.add(txtCity, cc.xy(3, 9));

				//---- lblAddress ----
				lblAddress.setText("Address");
				lblAddress.setHorizontalAlignment(SwingConstants.RIGHT);
				jPanelFields.add(lblAddress, cc.xy(1, 11));
				jPanelFields.add(txtAddress, cc.xy(3, 11));

				//---- lblPhoneNumber ----
				lblPhoneNumber.setText("Phone number");
				lblPhoneNumber.setHorizontalAlignment(SwingConstants.RIGHT);
				jPanelFields.add(lblPhoneNumber, cc.xy(1, 13));
				jPanelFields.add(txtPhoneNumber, cc.xy(3, 13));

				//---- label2 ----
				label2.setText("State");
				jPanelFields.add(label2, cc.xywh(1, 15, 1, 1, CellConstraints.RIGHT, CellConstraints.DEFAULT));

				//---- chkActive ----
				chkActive.setText("active");
				jPanelFields.add(chkActive, cc.xy(3, 15));

				//---- label3 ----
				label3.setText("* Required fields");
				label3.setForeground(Color.red);
				jPanelFields.add(label3, cc.xy(3, 17));

				//---- btnSave ----
				btnSave.setText("Update");
				jPanelFields.add(btnSave, cc.xywh(3, 19, 1, 1, CellConstraints.RIGHT, CellConstraints.DEFAULT));
			}
			panel1.add(jPanelFields);
		}
		add(panel1, cc.xy(1, 2));
		// JFormDesigner - End of component initialization  //GEN-END:initComponents
		lblTitle.setIcon(new ImageIcon(PATH+"users-icon.png"));
		btnSave.setIcon(new ImageIcon(PATH+"document-save.png"));
		btnDelete.setIcon(new ImageIcon(PATH+"edit-delete.png"));
		btnSearch.setIcon(new ImageIcon(PATH+"system-search.png"));
		this.listModelUsers=new DefaultListModel();
	}

	// JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
	// Generated using JFormDesigner Evaluation license - Guillermo Exposito
	private JPanel panel2;
	private JLabel lblTitle;
	private JPanel panel1;
	private JPanel jPanelSearchUser;
	private JLabel lblSearchUser;
	private JLabel label1;
	private JTextField txtSearch;
	private JButton btnSearch;
	private JScrollPane scrollPane1;
	private JList jListUsers;
	private JButton btnDelete;
	private JPanel jPanelFields;
	private JLabel lblName;
	private JTextField txtName;
	private JLabel lblUserName;
	private JTextField txtUserName;
	private JLabel lblCountry;
	private JTextField txtCountry;
	private JLabel lblCity;
	private JTextField txtCity;
	private JLabel lblAddress;
	private JTextField txtAddress;
	private JLabel lblPhoneNumber;
	private JTextField txtPhoneNumber;
	private JButton btnSave;
	private JCheckBox chkActive;
	private JLabel label2;
	private JLabel label3;
	private JLabel label4;
	private JLabel label5;
	// JFormDesigner - End of variables declaration  //GEN-END:variables
	
	private DefaultListModel listModelUsers;
	
	public JTextField getTxtName() {
		return txtName;
	}

	public void setTxtName(JTextField txtName) {
		this.txtName = txtName;
	}

	public JTextField getTxtUserName() {
		return txtUserName;
	}

	public void setTxtUserName(JTextField txtUserName) {
		this.txtUserName = txtUserName;
	}

	public JTextField getTxtCountry() {
		return txtCountry;
	}

	public void setTxtCountry(JTextField txtCountry) {
		this.txtCountry = txtCountry;
	}

	public JTextField getTxtCity() {
		return txtCity;
	}

	public void setTxtCity(JTextField txtCity) {
		this.txtCity = txtCity;
	}

	public JTextField getTxtAddress() {
		return txtAddress;
	}

	public void setTxtAddress(JTextField txtAddress) {
		this.txtAddress = txtAddress;
	}

	public JTextField getTxtPhoneNumber() {
		return txtPhoneNumber;
	}

	public void setTxtPhoneNumber(JTextField txtPhoneNumber) {
		this.txtPhoneNumber = txtPhoneNumber;
	}

	public JCheckBox getChkActive() {
		return chkActive;
	}

	public void setChkActive(JCheckBox chkActive) {
		this.chkActive = chkActive;
	}

	public JTextField getTxtSearch() {
		return txtSearch;
	}

	public void setTxtSearch(JTextField txtSearch) {
		this.txtSearch = txtSearch;
	}

	public JButton getBtnSearch() {
		return btnSearch;
	}

	public void setBtnSearch(JButton btnSearch) {
		this.btnSearch = btnSearch;
	}

	public JList getjListUsers() {
		return jListUsers;
	}

	public void setjListUsers(JList jListUsers) {
		this.jListUsers = jListUsers;
	}

	public JButton getBtnDelete() {
		return btnDelete;
	}

	public void setBtnDelete(JButton btnDelete) {
		this.btnDelete = btnDelete;
	}

	public JButton getBtnSave() {
		return btnSave;
	}

	public void setBtnSave(JButton btnSave) {
		this.btnSave = btnSave;
	}
	
	public DefaultListModel getListModelUsers() {
		return listModelUsers;
	}

	public void setListModelUsers(DefaultListModel listModelUsers) {
		this.listModelUsers = listModelUsers;
	}

	private class ActionSearchUsers implements ActionListener {
		public void actionPerformed(ActionEvent e) {

			try {
				controller.performSearchUsers();
				//DialogResource.showSaveSucessfully(btnSave.getLocation());
				
			} catch (RemoteException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

		}
	}
	
	private class ActionDeleteUser implements ActionListener {
		public void actionPerformed(ActionEvent e) {

			try {
				controller.performDeleteUser();
				//DialogResource.showSaveSucessfully(btnSave.getLocation());
				
			} catch (RemoteException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

		}
	}
	
	private class ActionUpdateUser implements ActionListener {
		public void actionPerformed(ActionEvent e) {

			try {
				controller.performUpdateUser();
				//DialogResource.showSaveSucessfully(btnSave.getLocation());
				
			} catch (RemoteException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

		}
	}
	
	private class ActionSelectItem implements ListSelectionListener{

		@Override
		public void valueChanged(ListSelectionEvent arg0) {
			try {
				controller.performSelectItem();
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		
	}

	
	
}